#!/usr/bin/perl

######################################################################
package Swignition::uF::hTodo;
######################################################################

use CGI::Util;
use Swignition::GenericParser::Utils;
use Swignition::uF;
use XML::LibXML qw(:all);
use strict;

sub parse_all
{
	my $page   = shift;
	my $within = shift || $page->{DOM};
	my @nodes  = searchClass('vtodo', $within);
	my @rv;
	
	foreach my $ev (@nodes)
	{
		push @rv, parse($page, $ev);
	}

	my @nodes = searchClass('vtodo-list', $within);
	foreach my $xoxo (@nodes)
	{
		push @rv, parse_xoxo($page, $xoxo);
	}
	
	return @rv;
} #/sub parse_all


sub uri
{
	my $this = shift;
	my $all  = shift;
	my @rv;
	
	if (length $this->{_id})
	{
		push @rv, Swignition::uF::TDBURI($this->{_page}->uri.'#'.$this->{_id});
	}
	if (lc($this->{_dom}->tagName) eq 'body')
	{
		push @rv, Swignition::uF::TDBURI($this->{_page}->uri);
	}
	push @rv, Swignition::GenericParser::Utils::BNodeURI($this->{_dom}, 'Todo')
		unless (@rv);
	
	return $rv[0] unless (defined $all);
	while ($all) { shift @rv; $all--; }
	return @rv;
} #/sub uri


sub rdf_subject
{
	return Swignition::uF::hEvent::rdf_subject(@_, 'Swignition::uF::hTodo');
}

sub metadata_ns
{
	return Swignition::uF::hEvent::metadata_ns(@_, 'Swignition::uF::hTodo');
}

sub parse
{
	return Swignition::uF::hEvent::parse(@_, 'Swignition::uF::hTodo');
} #/sub parse

sub parse_xoxo
{
	my $page   = shift;
	my $root   = shift;
	return [] unless ($root->tagName =~ /^(ul|ol)$/i);
	
	my @all_items;
	foreach my $li ($root->getChildrenByTagName('li'))
	{
		my @these_items = parse_xoxo_item($page, $li);
		push @all_items, @these_items;
	}
	
	return @all_items;
	
} #/sub parse_xoxo

sub parse_xoxo_item
{
	my $page   = shift;
	my $item   = shift;
	return [] unless ($item->tagName =~ /^(li)$/i);
	
	my $rv = { '_dom'=>$item };
	my $id = $item->getAttribute('id');
	$rv->{_id} = $id if (length $id);
	$rv->{_page} = $page;
	bless $rv;	
	
	my $node = $rv->{'_dom'}->cloneNode(1);
	
	my @child_items;
	foreach my $list ($node->getChildrenByTagName('ol'))
	{
		my @these_items = parse_xoxo($page, $list);
		push @child_items, @these_items;
		$node->removeChild($list);
	}
	foreach my $list ($node->getChildrenByTagName('ul'))
	{
		my @these_items = parse_xoxo($page, $list);
		push @child_items, @these_items;
		$node->removeChild($list);
	}

	my $parsed_htodo = parse($page, $node);
	foreach my $key (keys %$parsed_htodo)
	{
		$rv->{$key} = $parsed_htodo->{$key}
			unless ($key =~ /^_/);
	}
	
	unless (length $rv->{summary})
	{
		$rv->{summary} = STRINGIFY($node); 
		# NB: no value excerpting used here.
	}
	
	my @return = ($rv);
	foreach my $child (@child_items)
	{
		$child->{parent} = $rv->uri;
		push @{ $rv->{child} }, $child->uri;
		push @return, $child;
	}
	
	return @return;
}

1; #/package Swignition::uF::hTodo




